import * as API from '@/services';
import {
  KfBackendEnumPicker,
  DEFAULT_FORM_LAYOUT,
  KfBizQuickFormProps,
  KfFloatingContextProps,
  KfInput,
  KfQuickForm,
  KfTextArea,
} from 'kf-web-library';
import {Form} from 'antd';
import React, {FunctionComponent} from 'react';
import HxJournalColumnPicker from "@/components/HxJournalColumnPicker";

const FormItem = Form.Item;
type QueryEntity = API.JournalSpecialColumn;
type CreateEntity = API.JournalSpecialColumn_Create;
type ModifyEntity = API.JournalSpecialColumn_Modify;

type DataFormProps = {} & KfBizQuickFormProps & KfFloatingContextProps;

/**
 * 特色栏目聚焦 表单
 */
const QuickForm: FunctionComponent<DataFormProps> = (props) => {
  const {formType, bizId, ...rest} = props;

  return (
    <KfQuickForm
      formType={formType}
      formProps={{
        ...DEFAULT_FORM_LAYOUT,
      }}
      defaultQueryRequest={() =>
        API.queryJournalSpecialColumn(bizId!).then((resp) => {
          if (resp.flag) {
            resp.data = {
              ...resp.data,
              journalColumnIdList: resp.data?.journalColumnList?.map(item => item.id) || []
            } as any;
          }
          return new Promise((resolve) => resolve(resp));
        })
      }
      submitRequest={{
        create: (formValues) => API.createJournalSpecialColumn({...formValues} as CreateEntity),
        modify: (formValues) => API.modifyJournalSpecialColumn({...formValues, id: bizId} as ModifyEntity),
      }}
      {...rest}
    >
      <FormItem name={nameof<ModifyEntity>((x) => x.name)} label="名称" rules={[{required: true}]}>
        <KfInput/>
      </FormItem>
      <FormItem name={nameof<ModifyEntity>((x) => x.introduction)} label="简介" rules={[{required: false}]}>
        <KfTextArea/>
      </FormItem>
      <FormItem name={nameof<ModifyEntity>((x) => x.journalColumnIdList)} label="关联栏目" rules={[{required: true}]}>
        <HxJournalColumnPicker.TableSelect multiple={true}/>
      </FormItem>
    </KfQuickForm>
  );
};

export default QuickForm;
